using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{

    public class GameSrceen : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        
        int offsetX =0;
        int offsetY =0;
        int Is_selected = 0;
        int previous_x;
        int previous_y;

        Texture2D red;
        Texture2D blue;
        Texture2D green;
        Texture2D Movement;
        Texture2D Attack;
        Texture2D Unit1;
        Texture2D Unit2;


        public GameSrceen()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }


        protected override void Initialize()
        {
            this.IsMouseVisible = true;
            
            for (int i = 0; i < GameBoardProperty.Players.Length; i++)
            {
                GameBoardProperty.Players[i] = new Player();
                
            }

            base.Initialize();
        }


        protected override void LoadContent()
        {
            
            spriteBatch = new SpriteBatch(GraphicsDevice);

             blue = Content.Load<Texture2D>("blue");
             green = Content.Load<Texture2D>("green");
             red = Content.Load<Texture2D>("red");
             Movement = Content.Load<Texture2D>("red");//
             Attack = Content.Load<Texture2D>("green");

            Random random = new Random();
            ///
            ///Assign the image to each of tiles 
            ///
            for (int x = 0; x < GameBoardProperty.tiles.GetLength(0); x++)
            {
                for (int y = 0; y < GameBoardProperty.tiles.GetLength(1); y++)
                {
                    switch (random.Next(0,9)) //green a lot or not (0,adjust)
                    {
                        case 0: GameBoardProperty.tiles[x, y] = new Tile(green, new Vector2(x, y)); break;
                        default:
                            GameBoardProperty.tiles[x, y] = new Tile(blue, new Vector2(x, y));
                            GameBoardProperty.tiles[x, y]._LandPossible = true; // the unit can land or not for future implenment
                            break;
                    }
                }
            }

            //---------------------------------------------
            Unit1 = Content.Load<Texture2D>("Unit1");
            Unit2 = Content.Load<Texture2D>("Unit2");

            for (int i = 0; i < GameBoardProperty.Players.Length; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    switch (i)
                    {
                        case 0:
                            Unit unit1 = new Unit(Unit1);
                            GameBoardProperty.Players[0].Units.Add(unit1);
                            GameBoardProperty.tiles[random.Next(0, 19), random.Next(0, 15)].Unit = unit1;
                            break;
                        case 1:
                            Unit unit2 = new Unit(Unit2);
                            GameBoardProperty.Players[1].Units.Add(unit2);
                            GameBoardProperty.tiles[random.Next(0, 19), random.Next(0, 15)].Unit = unit2;
                            break;
                    }
                }
            }


            
        }

        protected override void UnloadContent()
        {
            
        }


        protected override void Update(GameTime gameTime)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            MouseState CurrentMouseState = Mouse.GetState();
            Coordinate.AccurateCoordinate(CurrentMouseState.X, CurrentMouseState.Y);
            MouseEvent.MouseAllEvents(CurrentMouseState);
            MouseEvent.PreMouseState = CurrentMouseState;
            


            base.Update(gameTime);
        }

        /// <summary>
        /// InMouseclick, we need to consider that only one tile that can be selected. Therefore, there is one "if" to check that.
        /// </summary>
        /// <param name="gameTime"></param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            this.spriteBatch.Begin();

            for (int y = 0; y < GameBoardProperty.tiles.GetLength(1); y++)
            {
                for (int x = 0; x < GameBoardProperty.tiles.GetLength(0); x++)
                {
                    this.spriteBatch.Draw(GameBoardProperty.tiles[x, y]._Texture, new Vector2(x * 40 + y % 2 * 20 + this.offsetX, y * 30 + this.offsetY), Color.White);
                    if (GameBoardState.State == GameBoardState.GameState.Attack)
                    {
                        if (GameBoardProperty.CheckDistance(GameBoardProperty.SelectedTile._Position, new Vector2(x, y)) <= GameBoardProperty.SelectedTile.Unit.Attack_Rang && GameBoardProperty.tiles[x, y].Unit != null)
                        {
                            this.spriteBatch.Draw(this.Attack, new Vector2(x * 40 + y % 2 * 20, y * 30), new Color(255, 255, 255, 50));
                        }
                    }
                    else if (GameBoardState.State == GameBoardState.GameState.Move)
                    {
                        if (GameBoardProperty.ReachTiles.IndexOf(GameBoardProperty.tiles[x, y]) != -1)
                        {
                            this.spriteBatch.Draw(this.Movement, new Vector2(x * 40 + y % 2 * 20, y * 30), new Color (255,255,255,50));
                        }
                    }

                     if(IsMousePass(x, y))
                     {
                         
                         spriteBatch.Draw(red, new Vector2(x * 40 + y % 2 * 20 + this.offsetX, y * 30 + this.offsetY), Color.White);
                     }
                     if(IsMouseClick(x,y))
                     {
                         
                         if (Is_selected == 0)
                         {
                             Is_selected = 1;
                             previous_x = x;
                             previous_y = y;
                             GameBoardProperty.tiles[x, y]._Texture = red;
                             //spriteBatch.Draw(GameBoardProperty.tiles[x, y]._Texture, new Vector2(x * 40 + y % 2 * 20 + this.offsetX, y * 30 + this.offsetY), Color.White);
                         }
                         else if (Is_selected == 1)
                         {
                             GameBoardProperty.tiles[previous_x, previous_y]._Texture = blue;
                             spriteBatch.Draw(GameBoardProperty.tiles[previous_x, previous_y]._Texture, new Vector2(x * 40 + y % 2 * 20 + this.offsetX, y * 30 + this.offsetY), Color.White);
                             GameBoardProperty.tiles[x, y]._Texture = red;
                             previous_x = x;
                             previous_y = y;
                             spriteBatch.Draw(GameBoardProperty.tiles[x, y]._Texture, new Vector2(x * 40 + y % 2 * 20 + this.offsetX, y * 30 + this.offsetY), Color.White);
                         }
                          
                     }
                     if (GameBoardProperty.tiles[x, y].Unit != null)
                     {
                         this.spriteBatch.Draw(GameBoardProperty.tiles[x, y].Unit.Texture, new Vector2(x * 40 + y % 2 * 20, y * 30 - 10), Color.White);
                     }

                }
                
            }
            this.spriteBatch.End();
            base.Draw(gameTime);
        }

        /// <summary>
        /// This two methods just return boolean value for mouse-over or mouse - click
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool IsMouseClick(int x, int y)
        {
            MouseState MS = Mouse.GetState();
            if (MS.LeftButton == ButtonState.Pressed)
            {
                return (MS.X >= (x * 40 + y % 2 *20) && MS.X <= (((x + 1)*40)+y %2 *20)  && MS.Y >= y * 30 && MS.Y <= (y + 1) * 30);
            }
            else
            {
                return false;
            }
            
        }

        public bool IsMousePass(int x, int y)
        {
            MouseState MS = Mouse.GetState();
            return (MS.X >= (x * 40 + y % 2 * 20) && MS.X <= (((x + 1) * 40) + y % 2 * 20) && MS.Y >= y * 30 && MS.Y <= (y + 1) * 30);
        }
    }
}
